package com.woniu.market_stall.web;


import com.github.pagehelper.PageInfo;
import com.woniu.market_stall.entity.Manager;
import com.woniu.market_stall.entity.Market;
import com.woniu.market_stall.entity.Role;
import com.woniu.market_stall.result.ResultResponse;
import com.woniu.market_stall.service.ManagerService;
import com.woniu.market_stall.utils.JwtUtil;
import com.woniu.market_stall.vo.AccountVo;
import com.woniu.market_stall.vo.JwtInfo;
import com.woniu.market_stall.vo.ManagerVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("manager")
public class ManagerController {

    @Autowired
    ManagerService managerService ;

    @PostMapping("/login")
    @ApiOperation(value = "本接口是登录")
    public ResultResponse<Manager> login(@RequestBody AccountVo accountVo){
        ResultResponse<Manager> result = null;
        try {
            Manager manager = managerService.login(accountVo.getUserName());
            if (manager == null){
                result = new ResultResponse<>(201,"账号不存在!");
            }else if (manager.getPassword().equals(accountVo.getUserPassword())){

                //登录成功  我把一个 标识写到前端  前端每次访问  把标识 带过来
                //后端服务器只要判断 请求中 有没有标识  如果有  就是已经登录  如果没有就是没有登录
                /*
                   这个标识 如何生成  ：
                   1、 唯一性
                   2、 存储的信息满足要求  被用户信息 存储进去
                   3、 具有安全性

                   这个标识 一般我们叫 令牌值（token）   我们一般使用插件生成 （JWT）
                 */

                JwtInfo jwtInfo = new JwtInfo();
                jwtInfo.setUserName(manager.getName());
                String token = JwtUtil.createToken(jwtInfo);
                result = new ResultResponse<>(200,"登录成功",manager,token);
            }else {
                result = new ResultResponse<>(202,"密码错误!");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(203,"系统错误，请联系管理员!");
        }
        return result ;
    }

    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody Manager manager){
        ResultResponse<Void> result = null;

        try {
            manager.setUpdateTime(new Date());
            int i=managerService.modifyByMid(manager);
            System.out.println(i);
            result = new ResultResponse<>(200,"修改成功!为了您的信息安全，请重新登陆！");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改管理员信息失败!");
        }
        return result ;
    }

    @GetMapping("/list")
    @ApiOperation(value = "本接口分页查询管理员")
    public ResultResponse<PageInfo<ManagerVo>> rlist(@ApiParam(value = "当前页的参数") @RequestParam(value = "now",defaultValue = "1") Integer now,
                                                     @ApiParam(value = "页面大小的参数") @RequestParam(value = "size",defaultValue = "3")Integer size,
                                                     @ApiParam(value = "按条件搜索管理员名称") String name){
        ResultResponse<PageInfo<ManagerVo>> result = null;
        try {
            PageInfo<ManagerVo> rolePageInfo = managerService.queryByPage(now, size, name);
            result = new ResultResponse<>(200,"查询成功!",rolePageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!");
        }
        return result ;
    }

    @PostMapping("/add")
    @ApiOperation(value = "本接口增加管理员")
    public ResultResponse<Void> add(@RequestBody ManagerVo managerVo){
        ResultResponse<Void> result = null;
        try {
            managerVo.setInsertTime(new Date());
            managerVo.setDeptId(managerService.findDeptId(managerVo.getDeptName()));
            managerService.add(managerVo);
            result = new ResultResponse<>(200,"添加管理员成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加管理员失败!");
        }
        return result ;
    }

    @GetMapping("/getRoleNamesByMid/{id}")
    @ApiOperation(value = "本接口查询管理员角色名")
    public ResultResponse<List<String>> getRoleNamesByMid(@PathVariable("id") int mid){
        ResultResponse<List<String>> result = null;

        try {
            List<String> names = managerService.getRoleNamesByMid(mid);
            result = new ResultResponse<>(200,"查询管理员成功!",names);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询管理员失败!");
        }
        return result ;
    }

    @PutMapping("/editManager")
    @ApiOperation(value = "本接口修改管理员")
    public ResultResponse<Void> editManager(@RequestBody ManagerVo managerVo){
        ResultResponse<Void> result = null;
        try {
            managerVo.setUpdateTime(new Date());
            managerVo.setDeptId(managerService.findDeptId(managerVo.getDeptName()));
            managerService.modify(managerVo);
            result = new ResultResponse<>(200,"修改管理员成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改管理员失败!");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "本接口逻辑删除管理员")
    public ResultResponse<Void> del(@PathVariable("id") Integer id){
        ResultResponse<Void> result = null;

        try {
            managerService.removeByKey(id);
            result = new ResultResponse<>(200,"删除管理员成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除管理员失败!");
        }
        return result ;
    }


    @GetMapping("/getManager")
    @ApiOperation(value = "本接口是查询所有正常的管理员的数据的")
    public ResultResponse<List<ManagerVo>> list(){
        ResultResponse<List<ManagerVo>> result = null;
        try{
            List<ManagerVo> ManagerVo = managerService.queryAllManager();
            result = new ResultResponse<>(200 , "查询状态正常的管理员成功！" ,ManagerVo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201 , "查询状态正常的管理员失败！" );
        }

        return result;
    }



}
